package ee.ts022.tuum.services.ws.endpoints;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import ee.ts022.tuum.services.CustomerService;
import ee.ts022.tuum.services.ws.generated.Contract;
import ee.ts022.tuum.services.ws.generated.Customer;
import ee.ts022.tuum.services.ws.generated.GetCustomerContracts;
import ee.ts022.tuum.services.ws.generated.GetCustomerContractsResponse;
import ee.ts022.tuum.services.ws.generated.GetCustomersResponse;
import ee.ts022.tuum.services.ws.utils.DomainSoapParser;

@Endpoint
public class CustomerServiceEndpoint {

	public static final String NAMESPACE_URI = "http://ts022.ee";
	private static final Logger logger = LoggerFactory.getLogger(CustomerServiceEndpoint.class);

	@Autowired
	CustomerService customerService;
	 
	@PayloadRoot(localPart = "getCustomers", namespace = NAMESPACE_URI)
	@ResponsePayload
	public GetCustomersResponse getCustomersResponse() {
		logger.info("GetCustomersResponse: getting all customers");
		GetCustomersResponse response = new GetCustomersResponse();
		List<Customer> customers = convertToSoapCustomers(customerService.getCustomers());
		response.getCustomers().addAll(customers);
		logger.info("GetCustomersResponse:  customers count "+customers.size());
		return response;
	}
	
	private List<Customer> convertToSoapCustomers(
			List<ee.ts022.tuum.domain.Customer> customers) {
		List<Customer> result = new ArrayList<Customer>();
		for (ee.ts022.tuum.domain.Customer customer : customerService.getCustomers()) {
			Customer c = new Customer();
			c.setFirstName(customer.getFirstName());
			c.setLastName(customer.getLastName());
			c.setCustomer(customer.getCustomer());
			c.setIdentityCode(customer.getIdentityCode());
			c.setBirthDate(DomainSoapParser.parseDate(customer.getBirthDate()));
			result.add(c);
		}
		return result;
	}

	@PayloadRoot(localPart = "getCustomerContracts", namespace = NAMESPACE_URI)
	@ResponsePayload
	public GetCustomerContractsResponse getCustomerContractsResponse(@RequestPayload GetCustomerContracts getCustomerContracts) {
		logger.info("GetCustomerContractsResponse: getting customer "+getCustomerContracts.getCustomer()+" contracts");
		GetCustomerContractsResponse getCustomerContractsResponse = new GetCustomerContractsResponse();
		List<Contract> contracts = convertToSoapContracts(customerService.getCustomerContracts(getCustomerContracts.getCustomer()));
		getCustomerContractsResponse.getContracts().addAll(contracts);
		logger.info("GetCustomerContractsResponse:  contracts count "+contracts.size());
		return getCustomerContractsResponse;
	}

	private List<Contract> convertToSoapContracts(
			List<ee.ts022.tuum.domain.Contract> customerContracts) {
		List<Contract> result = new ArrayList<Contract>();
		for (ee.ts022.tuum.domain.Contract contract : customerContracts) {
			Contract c = new Contract();
			c.setCntNumber(contract.getCntNumber());
			c.setConditions(contract.getConditions());
			c.setContract(contract.getContract());
			c.setContractStatusType(DomainSoapParser.parseContractStatusType(contract.getContractStatusType()));
			c.setContractType(DomainSoapParser.parseContractType(contract.getContractType()));
			c.setCreated(DomainSoapParser.parseDate(contract.getCreated()));
			c.setCreatedBy(DomainSoapParser.parseEmployee(contract.getCreatedBy()));
			c.setName(contract.getName());
			c.setDescription(contract.getDescription());
			c.setValidFrom(DomainSoapParser.parseDate(contract.getValidFrom()));
			c.setValidTo(DomainSoapParser.parseDate(contract.getValidTo()));
			c.setConditions(contract.getConditions());
			c.setNote(contract.getNote());
			c.setValueAmount(contract.getValueAmount());
			c.setCustomer(DomainSoapParser.parseCustomer(contract.getCustomer()));
			result.add(c);
		}
		return result;
	}
}
